package io.github.zhengyue.demo.config;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

/**
 * <h3>springboot-shiro</h3>
 * <p></p>
 *
 * @author : zhengyue
 * @date : 2020-11-19 13:54
 **/
public class UserRealm extends AuthorizingRealm {
    // 授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("授权");
        return null;
    }

    // 认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        System.out.println("认证");

        // 用户名，密码
        String name = "admin";
        String password = "000000";
        UsernamePasswordToken token = (UsernamePasswordToken) authenticationToken;
        if(!token.getUsername().equals(name)) {
            return null; // 抛出异常
        }
        // 密码认证，shiro做
        return new SimpleAuthenticationInfo("", password, "");
    }
}
